package org.vectrics.web.struts;


import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.ecs.html.A;
import org.apache.log4j.Category;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.company.Company;
import org.vectrics.company.CompanySelectCriteria;
import org.vectrics.company.web.struts.CompanyPageList;
import org.vectrics.company.web.struts.SaveCompanyAction;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.PageListTag;
import org.vectrics.struts.Request;


public abstract class CompanyManagerTag extends PageListTag {

	private static final long serialVersionUID = 6293553642917853573L;

	public final static String TRUE = "true";
	public final static String FALSE = "false";

	private static Category log = Category.getInstance(CompanyManagerTag.class);
    public String PAGE_KEY_PARAM = "page_key";
    private String showInactive = "false";
    private String showOnlyParents = "false";
    
    public String getDescription() {
        log.debug("getDescription() called.");
        return(null);
    }

    public CompanyManagerTag() {
    	super();
    }

    protected PageList createPageList() {
    	CompanySelectCriteria companySelectCriteria = new CompanySelectCriteria();
		Set domains = CoreServiceLocator.getPersonService()
			.findDomainsWithUserPrivilege(this.getAuthPerson(), Company.PRIVILEGE_COMPANY_VIEW);
		companySelectCriteria.setDomains(domains);

    	PageList pageList = new CompanyPageList(companySelectCriteria);
    	return(pageList);
    }



    protected void makeHeaderCells(Row row) {
        log.debug("makeHeaderCells() called.");

        Cell cell = row.addCell();
        //cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", "Company Code"));

        cell = row.addCell();
		//cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", "Company Name"));

        cell = row.addCell();
		//cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", "City"));

        cell = row.addCell();
		//cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", "Options"));
    }


    protected void makeObjectRow(Request request, Row row, Company company) {
    	log.debug("makeObjectRow()");
        Cell cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", company.getReferenceId()));

        cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", company.getName()));

        cell = row.addCell();
        if (company.getAddress() != null) {
            cell.addElement(TextHelper.getFont("subFont", company.getAddress().getCity()
                    + ", " + company.getAddress().getState()));
        }

        cell = row.addCell();
        A link = new A();
        link.setHref(request.getContextPath() + "/admin/company/view.do?"
        		+ SaveCompanyAction.ID_PARAM + "=" + company.getId());
        link.setClass("subLink");
        link.addElement(this.getPhrase("phrase.view"));
        cell.addElement(link);
        cell.addElement("&nbsp");

        link = new A();
        link.setHref(request.getContextPath() + "/admin/company/remove.do?"
        		+ SaveCompanyAction.ID_PARAM + "=" + company.getId());
        link.setClass("subLink");
        link.addElement("remove");
        cell.addElement(link);
    }

    protected void setTableProperties(BorderTable table) {
        table.setWidth(500);
    }


  //  abstract protected PageList getPageList();


    /*
    protected String doRender(Request request, Properties attributes) {
        log.debug("doRender() called.");
        Table mainTable = new Table();
        Row mainRow = mainTable.addRow();
        Cell mainCell = mainRow.addCell();

        log.debug("doRender() - getting current page from derived.");
        String formPath = request.getHttpRequest().getRequestURI();
        Collection companyList = null;

        if (request.getParameter(PAGE_KEY_PARAM) != null) {
            String currentUserName = request.getParameter(PAGE_KEY_PARAM);
            getPageList().setCurrentKey(currentUserName);
        }

        companyList = getPageList().getPage();

        mainCell.addElement(makePrevNextLinks());
        showList(getDescription(), mainCell, companyList);

        mainCell.addElement(makePrevNextLinks());

        //showList(getDescription(), mainCell, companyList);
        return(mainTable.toString());
    }
*/


    protected abstract String getNewCompanyLink(Request request);



    protected void showList(String description, Cell mainCell, List companyList, Form form)
    {
        log.debug("showList() called.");

        Iterator companyIter = companyList.iterator();

        BorderTable table = new BorderTable();

        table.setID("border_table");
        table.setWidth(600);
        mainCell.addElement(table);


        if (companyIter.hasNext()) {
            Row row = table.addRow();
            row.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");
            makeHeaderCells(row);
            while (companyIter.hasNext()) {
				Company company = (Company)companyIter.next();
				try {
	                row = table.addRow();
	                makeObjectRow(this.getRequest(), row, company);
				}
				catch (Exception de) {
					log.error("Exception finding company", de);
				}

            }
        }
        else {
            log.debug("doRender() - no users.");
            Row row = table.addRow();
            Cell cell_1 = row.addCell();
            cell_1.addElement(TextHelper.getFont("subFont", "<I>No Companies</I><br>"));
        }



        mainCell.addElement("<br>&nbsp;");
        A addLink = new A();
        addLink.setHref(getNewCompanyLink(this.getRequest()));
        addLink.setClass("subLink");
        addLink.addElement("New Company");
        mainCell.addElement(addLink);
        mainCell.addElement("<br><br>");

    }


	/**
	 * @return Returns the showInactive.
	 */
	public String getShowInactive() {
		return showInactive;
	}

	/**
	 * @param showInactive The showInactive to set.
	 */
	public void setShowInactive(String showInactive) {
		this.showInactive = showInactive;
	}
	/**
	 * @return Returns the showOnlyParents.
	 */
	public String getShowOnlyParents() {
		return showOnlyParents;
	}

	/**
	 * @param showOnlyParents The showOnlyParents to set.
	 */
	public void setShowOnlyParents(String showOnlyParents) {
		this.showOnlyParents = showOnlyParents;
	}

}